<?php
namespace Admin\Controller;

class CustomAgreementController extends CommonController{
	public  $exec_comfunc  = 'index,add,edit,delete,del,recycle';//index,add,edit,delete
	
	public function _initialize(){
        parent::_initialize();
        $type = $this->_get('type');
        $this->_db_where['type'] = $type;
        
    }
    public function index(){
        // 按类别搜索
        $category_id = $this->_get('category_id');
        if($category_id){
            $where['category_id'] = $category_id;
        }
        // 按标题搜索
        $title = $this->_get('title');
        if($title){
            $where['title'] = array('like', '%' . $title . '%');
        }
        $this->assign('title', $title);
        
        $this->assign('info', $where);
        $where['is_delete'] = 0;
        parent::index('', $where);
    }


    public function package_setting(){
        $id = $this->_get('id', '对不起，您的操作有误！');
        $info = D('CustomAgreement')->find($id);
        if(IS_POST){
            $agreement_ids = $this->_post('agreement_ids', '请选择套餐合同！');
            $package_price = $this->_post('package_price', '请填写套餐价格！');
            foreach ($agreement_ids as $agreement_id) {
                if($agreement_id != $id){
                    $agreement_id_arr[] = $agreement_id;
                }
            }
            // 更新合同套餐信息
            D('CustomAgreement')->where(array('id' => $id))->save(array(
                'package_agreement_ids' => implode(',', $agreement_id_arr),
                'package_price'         => $package_price
            ));

            $this->success('配置成功！');
        }
        $this->assign('list', D('CustomAgreement')->get_list(array('id' => array('in', $info['package_agreement_ids']))));
        $this->assign('info', $info);
        $this->display();
    }

    // 自动完成处理
    public function autocomplete(){
        $query = htmlspecialchars(trim($this->_get('query')));
        $where['title'] = array('like', "%$query%");

        $not_ids = $this->_get('not_ids');
        if($not_ids){
            $where['id'] = array('not in', $not_ids);
        }
        $list = D('CustomAgreement')->where($where)->limit("0,10")->select();
        $data = array();
        if($list){
            foreach($list as $rs){
                $rs['title'] = $rs['title'] . '(' . $rs['service_price'] . ')';
                $rs['value'] = $rs['id'];
                $data[] = $rs;
            }
        }
        $data = json_encode($data);
        exit($data);
    }
}